package com.rc.evcharger.service.market;

import com.rc.evcharger.mapper.market.MarketActivityMemberMapper;
import com.rc.evcharger.model.market.MarketActivity;
import com.rc.evcharger.model.market.MarketActivityMember;
import com.rc.evcharger.model.market.MarketActivityMemberExample;
import com.rc.evcharger.model.market.MarketActivityRecord;
import com.rc.evcharger.model.order.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: yang
 * @Date: 2022/7/28 0:48
 */
@Service
public class MarketActivityMemberServiceImpl implements MarketActivityMemberService {

    @Autowired
    private MarketActivityMemberMapper marketActivityMemberMapper;

    @Override
    public MarketActivityMemberMapper getMapper() {
        return marketActivityMemberMapper;
    }

    /**
     * 参与活动的用户
     *
     * @param orderInfo
     * @param marketActivityRecord
     * @return
     */
    @Override
    public MarketActivityMember insertByOrderInfoAndMarketActivity(OrderInfo orderInfo, MarketActivityRecord marketActivityRecord) {

        MarketActivity marketActivity = marketActivityRecord.getJoinMarketActivity();

        MarketActivityMemberExample example = new MarketActivityMemberExample();
        example.createCriteria().andMarketActivityIdEqualTo(marketActivityRecord.getMarketActivityId())
                .andMemberIdEqualTo(orderInfo.getMemberId());
        List<MarketActivityMember> marketActivityMembers = marketActivityMemberMapper.selectByExample(example);

        int size = 0;
        if (marketActivityMembers != null) {
            size = marketActivityMembers.size();
        }

        //活动次数达到满返条件,将不会再次参与
        if (size >= marketActivity.getMaxActivityCount()) {
            return null;
        }

        //累充条件( 0:活动期间内满足,   1:一次性满足 默认0)
        if ("1".equals(marketActivity.getSumCondition()) && size >= 1) {
            return null;
        }

        MarketActivityMember marketActivityMember = new MarketActivityMember();
        marketActivityMember.setMarketActivityId(marketActivity.getMarketActivityId());
        marketActivityMember.setMemberId(orderInfo.getMemberId());
        marketActivityMember.setCreatedTime(new Date());
        marketActivityMember.setLastUpdatedTime(new Date());
        marketActivityMember.setGainCount(1);
        marketActivityMember.setActivityType(marketActivity.getActivityType());

        //满返则关联优惠券
        if ("4".equals(marketActivity.getActivityType())) {
            marketActivityMember.setTicketTypeId(marketActivityRecord.getTicketTypeId());
        }
//        marketActivityMemberMapper.insert(marketActivityMember);

        return marketActivityMember;
    }

    @Override
    public List<MarketActivityMember> selectByMemberId(Long memberId,String activityType) {
        return marketActivityMemberMapper.selectByMemberId(memberId,activityType);
    }
}
